<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIManager
 *
 * @author jianfeng
 */
abstract class BUIScreen extends BUIBase {

    protected $screenParent = null;
    protected $uiNode = null;
    protected $perviousScreen = null;
    protected $nextScreen = null;
    
    

    public function __construct($parent = NULL) {

        parent::__construct();

        if ($parent != null)
            $this->screenParent = $parent;
    }

    /**
     *
     * @return BUINode 
     */
    public function getUINode() {
        if ($this->screenParent instanceof BUIScreenFlow)
            return $this->screenParent->getUINode();

        return $this->screenParent;
    }

    /**
     *
     * @param type $uiNode 
     */
    public function setUINode($uiNode) {

        $uiNode = Blyn::app()->getCurrentNode()->getUIManager();

        if ($this->screenParent instanceof BUIScreenFlow)
            $this->screenParent->setUINode($uiNode);
        else {
            $this->screenParent = $uiNode;
        }
    }

    public function getParent() {
        return $this->screenParent;
    }

    public function setParent($parent) {
        $this->screenParent = $parent;
    }

    public function render($data = null) {
        $currentUINode = Blyn::app()->getCurrentNode()->getUIManager();
        $currentUINode->setCurrentScreen($this);
    }

    public function getName() {
        $className = get_class($this);
        if (substr($className, 0, 7) == 'BScreen')
            $screenName = substr($className, 7);
        else {
            $screenName = $className;
        }
        return $screenName;
    }

    public function registerMenu($event) {
        ;
    }

    public function isMemberOfScreenFlow() {
        if ($this->screenParent instanceof BUIScreenFlow)
            return TRUE;

        return FALSE;
    }

    public function getUrl($data = NULL) {
        if ($data == NULL)
            $data = array();

        $data['blyn_scn'] = get_class($this);

        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        $url = $this->getUINode()->getUrl($data);

        return $url;
    }

    public function getLinkUrl($data) {
        $url = $this->getUrl($data);

        $route = $url[0];
        unset($url[0]);

        $url = yii::app()->getController()->createUrl($route, $url);

        return $url;
    }

    public function getLayoutView() {
        if ($this->layoutView == NULL)
            $this->layoutView = 'screenLayout';

        return $this->layoutView;
    }

    public function startRender($config = NULL) {

        $this->beforeStartRender();

        $controller = yii::app()->getController();

        $request = yii::app()->request;

        $views = array();

        if ($request->isAjaxRequest) {

            //first get views from design time ajaxlink
            $ajaxViews = Blyn::app()->getCurrentNode()->getUIManager()->getTransitVariable('bilyn_ajaxViews');
            //can also set ajaxView during runtime
            $ajaxViews2 = $this->getAjaxViews();

            $ajaxLink = isset($_REQUEST['bilyn_ajaxLink']) ? $_REQUEST['bilyn_ajaxLink'] : NULL;
            
            $findJs = strpos('j_', $ajaxLink);
            
            if($findJs === FALSE)
                $ajaxLink_j = 'j_'.$ajaxLink;
            else {
                $ajaxLink_j= $ajaxLink;
                $ajaxLink = substr($ajaxLink_j,2);
            }

            if ($ajaxLink != NULL || $ajaxLink_j != NULL) {
                if (isset($ajaxViews[$ajaxLink]))
                    $views = $ajaxViews[$ajaxLink];
                if (isset($ajaxViews[$ajaxLink_j]))
                    $views = $ajaxViews[$ajaxLink_j];
                //then get views from runtime views
                if (isset($ajaxViews2[$ajaxLink]))
                    $views = $ajaxViews2[$ajaxLink];
                if (isset($ajaxViews2[$ajaxLink_j]))
                    $views = $ajaxViews2[$ajaxLink_j];
            }

            //by default, set 'startPage' as start point of ajaxView            
            if ($views == NULL) {
                array_push($views, array('ajaxView' => 'startPage'));
            }

            foreach ($views as $aView) {

                $view = $aView['ajaxView'];

                $data = array();
                if (isset($aView['data']))
                    $data = $aView['data'];

                $this->renderView($view, $data);
            }

            yii::app()->end();
        }

        $layoutView = isset($config['layoutView']) ? $config['layoutView'] : NULL;
        $viewsData = isset($config['views']) ? $config['views'] : NULL;

        $controller->render('startPage', array('layoutView' => $layoutView, 'views' => $viewsData));
    }

    public function beforeStartRender() {

        $tranObj = Blyn::app()->getTransitObj();

        //$transitApp = Blyn::app()->getTransitApp();
        $currentUINode = Blyn::app()->getCurrentNode()->getUIManager();

        if ($this->isMemberOfScreenFlow()) {
            $sf = $this->getParent();
            $sf->addTransitVariable(BUIScreenFlow::TRANSITVARIABLE_SCREENFLOW_INDEX, $sf->getIndex());
            //$sf->addTransitVariable(BTransitObj::VariableName_Id, $sf->getId());
            //$sf->addTransitVariable(BTransitObj::VariableName_Class, $sf->getName());
            $tranObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowId, $sf->getId());
            $tranObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowClass, $sf->getName());

            $tranObj->resetTransitVariable(BTransitObj::Scope_Screen);
        } else {
            $tranObj->resetTransitVariable(BTransitObj::Scope_ScreenFlow);
            //$tranObj->addTransitVariable(BTransitObj::VariableName_CurrentScreen, BTransitObj::VariableName_Id, $this->getId());
            //$tranObj->addTransitVariable(BTransitObj::VariableName_CurrentScreen, BTransitObj::VariableName_Class, get_class($this));
            $tranObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenId, $this->getId());
            $tranObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenClass, get_class($this));
        }

        $tranObj->uploadTransitVariable();
    }

    public function getId() {
        $uiNodeId = Blyn::app()->getCurrentNode()->getUIManager()->getId();
        return $uiNodeId . '_' . $this->getName();
    }
    
    
    
    public function addTransitVariable($name, $value, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getTransitObj();

        /*
        if ($name == BTransitObj::VariableName_Id)
            return $tObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowId, $value);
        if ($name == BTransitObj::VariableName_Class)
            return $tObj->addTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowClass, $value);
         * 
         */

        $tObj->addTransitVariable(BTransitObj::Scope_Screen, $name, $value);
    }

    public function getTransitVariable($name, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getTransitObj();

        /*
        if ($name == BTransitObj::VariableName_Id)
            return $tObj->getTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowId);
        if ($name == BTransitObj::VariableName_Class)
            return $tObj->getTransitVariable(BTransitObj::Scope_AppMeta, BTransitObj::V_CurrentScreenFlowClass);
         * 
         */

        return $tObj->getTransitVariable(BTransitObj::Scope_Screen, $name);
    }

    public function resetTransitVariable($name = NULL) {

        $tObj = Blyn::app()->getTransitObj();
        /**
         * for scope reset, only happen when id changed
         */
        if ($name == NULL) {
            $tId = $this->getTransitVariable(BTransitObj::VariableName_Id);
            $myId = $this->getId();
            if ($tId != $myId) {
                $tObj->resetTransitVariable(BTransitObj::Scope_Screen);                
                $this->addTransitVariable(BTransitObj::VariableName_Id, $myId);
            }
        } else {           
            $tObj->resetTransitVariable(BTransitObj::Scope_Screen, $name);
        }
    }

}

?>
